package com.manager.core.module.manager.service;

import java.util.ArrayList;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.manager.commons.mybatis.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.manager.commons.utils.StringUtil;
import com.manager.commons.mybatis.dao.GenericDao;
import com.manager.core.module.manager.dao.PlatformAttributesDao;
import com.manager.core.module.manager.entity.PlatformAttributes;
import com.manager.core.web.json.AjaxJson;

/**
 * PlatformAttributesservice
 *
 * @author H
 * @version 1.0
 */
@Service
public class PlatformAttributesService extends GenericService<PlatformAttributes> {

    @Autowired
    private PlatformAttributesDao jdbcDao;

    @Override
    public GenericDao<PlatformAttributes> getDao() {
        return jdbcDao;
    }
    public PlatformAttributes findView(Map<String, Object> params) {
        return jdbcDao.findMap(params);
    }
    /**
     * 保存
     *
     * @param params
     * @return
     */
    @Override
    public AjaxJson save(Map<String, Object> params) {
        try {
            if (null != params) {
                int f = 0;
                String _id = String.valueOf(params.get("_id"));
                if (StringUtil.isEmpty(_id)) {
                    f = insertMap(params);
                } else {
                    f = updateMap(params);
                }
                return new AjaxJson(f, "保存".concat(f > 0 ? "成功" : "失败"));
            }
            return new AjaxJson("参数错误");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return new AjaxJson("异常错误，请稍后再试");
    }


    /**
     * 查询json格式数据
     *
     * @param params
     * @return
     */
    public String querySelect(Map<String, Object> params) {
        List<PlatformAttributes> list = jdbcDao.queryStoreSelect(params);

        List<Map<String, String>> result = new ArrayList<>();

        for (PlatformAttributes attr : list) {
            // code 代表返回code或者id 其他则返回value
            boolean code = params.containsKey("code");
            result.add(new HashMap<String, String>() {
                {
                    put("text", attr.getPaName());
                    if (code) {
                        put("value", attr.getPaCode());
                    } else {
                        put("value", attr.getPaValue());
                    }

                }
            });
        }
        return querySelect(result, params);
    }

    @Override
    public PlatformAttributes getModel() {
        return new PlatformAttributes();
    }
}